package com.example.superoa.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSONArray;
import com.example.superoa.model.pojos.Staff;
import com.example.superoa.model.service.AttendMonthService;
import com.example.superoa.model.service.ChaxunService;
import com.example.superoa.model.service.LoginService;
import com.example.superoa.model.service.PositionService;
import com.example.superoa.model.service.StaffService;
import com.example.superoa.model.vo.LoginVo;

@Controller
@SessionAttributes({"staff","funsId","sid","name","poin","yhusfzz"})
public class LoginController {
	@Autowired
	private LoginService ls;
	@Autowired
	private PositionService ps;
	@Autowired
	private ChaxunService cs;
	@Autowired
	private AttendMonthService ser;
	@Autowired
	private StaffService ss;
	
	@RequestMapping("login")
	public void login(){
		
	}
	//考勤统计图
	@RequestMapping("kaoqing-tongji")
	@ResponseBody
	public JSONArray tongji(HttpSession session) {
		Integer sid=Integer.parseInt(session.getAttribute("sid").toString());
		return ls.tongji(sid);
	}
	@RequestMapping("loginok")
	@ResponseBody
	public int loginok(String staff_name,String staff_pwd,Model model){
		LoginVo lv=ls.login(staff_name, staff_pwd);
		String sfzz=ss.staffById(Integer.valueOf(lv.getStaff_id())).getStaff_sfzz();
		model.addAttribute("yhusfzz", sfzz);
		if(lv!=null&&sfzz.equals("是")) {
			System.err.println("loginok:"+lv.getStaff_id());
			model.addAttribute("staff", lv);
			model.addAttribute("sid", lv.getStaff_id());
			model.addAttribute("name", lv.getStaff_name());
			model.addAttribute("poin",ser.select(Integer.parseInt(lv.getStaff_id())));
			return 1;
		}else {
			return 2;
		}
	}

	@RequestMapping("tomainhome")
	public String tomainadministrator(Model model){
				return "redirect:main-administrator";
	}
	@RequestMapping("main-administrator")
	public void mainadministrator(Model model,HttpSession session){
		//包括1级和1级下面的二级
		System.err.println("进入后："+session.getAttribute("sid"));
		int i=Integer.valueOf((String) session.getAttribute("sid"));
		model.addAttribute("fun1", ps.allFunsByUser());
		//根据用户查询(需要登陆的时候查询并存储)
		model.addAttribute("funsId", ps.allFunsByUser(i));
	}
	
	@RequestMapping("zhuxiao")//注销
	public void exits(HttpSession session,HttpServletResponse response) {
		System.err.println("之前："+session.getAttribute("staff"));
		session.removeAttribute("staff");
		session.invalidate();
		System.err.println("之后："+session.getAttribute("staff"));
		try {
			response.sendRedirect("login");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
